<?php

namespace APP\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Model\GoldBeanModel;
use App\Model\SignModel;
use App\Model\WebUserModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Model\GoldRecordModel;
use App\Model\PkRelModel;
class MainController extends Controller
{
    public function index()
    {
        return view('Admin/user/index');
    }


    public function search(Request $request){

        $where = [];
        //返回数组
        $res_data = array(
            'total' => 0,
            'rows' => array()
        );

        if($request->input('startDateTime') || $request->input('endDateTime')){

            if($request->input('startDateTime')) {
                $where[] = ['dtTime', '>', $request->input('startDateTime')." 00:00:00"];
            }
            if($request->input('endDateTime')) {
                $where[] = ['dtTime', '<', $request->input('endDateTime')." 23:59:59"];
            }
            if ($request->input('name')) {
                $where[] = ['sUserName','like','%'.$request->input('name').'%'];
            }

            $groups = PkRelModel::where($where)
                ->leftJoin('webuser','webuser.iSeqId','=','pkrel.master_userid')
                ->groupBy("pkrel.master_userid")
                ->select(DB::raw('tb_pkrel.master_userid,tb_webuser.iSeqId,tb_webuser.iRegistTime,
                tb_webuser.sUserImage,tb_webuser.sUserName,tb_webuser.iAllPk'))
                ->orderBy($request->input('sort'), $request->input('order'))
                ->paginate($request->input('limit'))
                ->toArray();

            if($groups['data']){
                $res_data['total'] = $groups['total'];
                foreach ( $groups['data'] as &$row ) {
                    $signtime = SignModel::select(DB::raw('dtModifyTime'))
                        ->where(["iUserId"=>$row['iSeqId']])
                        ->orderBy("dtModifyTime","DESC")
                        ->first();
                    $res_data['rows'][] = array(
                        'id'          => $row['iSeqId'],
                        'iRegistTime' => $row['iRegistTime'],
                        'sUserImage'  => isset($row['sUserImage']) ? '<image src='.$row['sUserImage'].' style="width: 30px;height: 30px;"></image>' : '',
                        'sUserName'   => !empty(urldecode($row['sUserName'])) ? urldecode($row['sUserName']) : '',
                        'dtModifyTime'=> empty($signtime->dtModifyTime) ? 0 : $signtime->dtModifyTime,
                        'iAllPk'      => $row['iAllPk'],
                        'iAll'        => '-',
                        'iWinAll'     => '-',
                        'signgold'    => '-',
                        'exchangegold'=> '-',
                        'pkgold'      => '-',
                        'rate'        => '-',
                    );
                }
            }


        }else{

            if ($request->input('name')) {
                $where[] = ['sUserName','like','%'.$request->input('name').'%'];
            }
            if ($request->input('startDate') && $request->input('endDate')) {
                if($request->input('startDate') <= $request->input('endDate')){
                    $where[] = ['iAllPk','>',$request->input('startDate')];
                    $where[] = ['iAllPk','<',$request->input('endDate')];
                }
            }
            $groups = WebUserModel::where($where)
                ->leftJoin('sign','webuser.iSeqId','=','sign.iUserId')
                ->select(DB::raw('tb_sign.dtModifyTime,tb_webuser.iSeqId,tb_webuser.iRegistTime,
            tb_webuser.sUserImage,tb_webuser.sUserName,tb_webuser.type,tb_webuser.iAllPk,tb_webuser.iAll,(tb_webuser.iWinAll/tb_webuser.iAll) as rate'))
                ->groupBy("webuser.iSeqId")
                ->orderBy($request->input('sort'), $request->input('order'))
                ->paginate($request->input('limit'))
                ->toArray();

            if($groups['data']){
                $res_data['total'] = $groups['total'];
                foreach ( $groups['data'] as &$row ) {

                    $goldbean = GoldBeanModel::where('iUserId',$row['iSeqId'])->first();

                    $signgold = GoldRecordModel::select(DB::raw('sum(iGoldBeanConsume) as signgold'))
                        ->where(["iUserId"=>$row['iSeqId'],"iConsumeType"=>4])
                        ->first();
                    $exchangegold = GoldRecordModel::select(DB::raw('sum(iGoldBeanConsume) as exchangegold'))
                        ->where([ "iUserId"=>$row['iSeqId'],"iConsumeType"=>3])
                        ->first();
                    $pkgold = GoldRecordModel::select(DB::raw('sum(iGoldBeanConsume) as pkgold'))
                        ->where(["iUserId"=> $row['iSeqId']])
                        ->where("iConsumeType",'<', 3)
                        ->first();


                    $b_operation = '';
                    $a_operation = '';
                    $c_operation = '';

                    if($goldbean->iNowGoldBean > 0){
                        $a_operation .= '&nbsp;&nbsp;<a href="javascript: void(0);" onclick="user.wing('.$row['iSeqId'].')">详情</a>';
                    }
                    if($exchangegold->exchangegold < 0){
                        $b_operation .= '&nbsp;&nbsp;<a href="javascript: void(0);" onclick="user.change('.$row['iSeqId'].')">详情</a>';
                    }
                    if($row['type']==1){
                        $c_operation .= '&nbsp;&nbsp;<a href="javascript: void(0);" onclick="user.allChange('."'all'".')">查看所有</a>';
                    }

                    $res_data['rows'][] = array(
                        'id'          => $row['iSeqId'],
                        'iRegistTime' => $row['iRegistTime'],
                        'sUserImage'  => isset($row['sUserImage']) ? '<image src='.$row['sUserImage'].' style="width: 30px;height: 30px;"></image>' : '',
                        'sUserName'   => !empty(urldecode($row['sUserName'])) ? urldecode($row['sUserName']) : '',
                        'dtModifyTime'=> $row['dtModifyTime'],
                        'iAllPk'      => $row['iAllPk'],
                        'iAll'        => $row['iAll'],
                        'iWinAll'     => $goldbean->iNowGoldBean > 0 ? $goldbean->iNowGoldBean .$a_operation:0,
                        'signgold'    => empty($signgold->signgold) ? 0 : $signgold->signgold,
                        'exchangegold'=> (empty($exchangegold->exchangegold) ? 0 : $exchangegold->exchangegold).$b_operation.$c_operation,
                        'pkgold'      => empty($pkgold->pkgold) ? 0 : $pkgold->pkgold,
                        'rate'        => empty($row['rate']) ? 0 :ceil($row['rate']*100).'%',
                    );
                }
            }
        }
        return response()->json($res_data);
    }

}